<style>
    .price, .priced, .total, .totald, #grandtotal, #grandtotald{
        display: none;
    }
</style>
<script language="javascript" type="text/javascript">
	
//    Number.prototype.formatMoney = function(dec, thouS, decS){
//        dec = isNaN(dec = Math.abs(dec)) ? 2 : dec;
//        decS = decS == undefined ? "." : decS;
//        thouS = thouS == undefined ? "," : thouS;
//
//        var n = this,
//            sign = n < 0 ? "-" : "",
//            i = parseInt(n = Math.abs(+n || 0).toFixed(dec)) + "",
//            j = (j = i.length) > 3 ? j % 3 : 0;
//
//        return sign + (j ? i.substr(0, j) + thouS : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouS) + (dec ? decS + Math.abs(n - 1).toFixed(dec).slice(2) : "");
//    };

    function formatNumber (obj, decimal) {
        //decimal  - the number of decimals after the digit from 0 to 3
        //-- Returns the passed number as a string in the xxx,xxx.xx format.
        anynum = eval(obj);
        divider =10;
        switch(decimal){
            case 0:
                divider =1;
                break;
            case 1:
                divider =10;
                break;
            case 2:
                divider =100;
                break;
            default:       //for 3 decimal places
                divider =1000;
        }

        workNum=Math.abs((Math.round(anynum*divider)/divider));
        workStr=""+workNum
        if (workStr.indexOf(",")==-1){workStr+=","}
        dStr=workStr.substr(0,workStr.indexOf(","));
        dNum=dStr-0
        pStr=workStr.substr(workStr.indexOf(","))

        while (pStr.length-1< decimal){
            pStr+="0"
        }

        if(pStr ==',') pStr ='';
        //--- Adds a comma in the thousands place.
        if (dNum>=1000) {
            dLen=dStr.length
            dStr=parseInt(""+(dNum/1000))+"."+dStr.substring(dLen-3,dLen)
        }
        //-- Adds a comma in the millions place.
        if (dNum>=1000000) {
            dLen=dStr.length
            dStr=parseInt(""+(dNum/1000000))+"."+dStr.substring(dLen-7,dLen)
        }

        retval = dStr + pStr
        //-- Put numbers in parentheses if negative.
        if (anynum<0) {retval="("+retval+")";}
        //You could include a dollar sign in the return value.
        //retval =  "$"+retval
        return retval;
    }
        
    function submit_item() {
        try {
            CalculateTotal();
            
            var namas = jQuery('.nama');
            var items = jQuery('.item');
            var totals = jQuery('.total');
            var prices = jQuery('.price');
            var totalds = jQuery('.totald');
            var priceds = jQuery('.priced');
            var qtys = jQuery('.qty');
            var satuans = jQuery('.satuan');
            jQuery.each(totals, function(i){
                if(jQuery(qtys[i]).val() != 0)
                    parent.parent.append_table(
                        {
                            qty: jQuery(qtys[i]).val(), satuan: jQuery(satuans[i]).html(), nama: jQuery(namas[i]).html(), item: jQuery(items[i]).html(),
                            price: jQuery(prices[i]).html(),total: jQuery(totals[i]).html(), priced: jQuery(priceds[i]).html(),totald: jQuery(totalds[i]).html()
                        }
                    );
            });
        }
        catch(e) { alert(e.message); }
        parent.parent.focus();

        close_gb();
                 
    }
    
    function close_gb(){
        <?php echo CloseGB(false); // close greybox   ?>
    }
    
    function UpdateTotals(id) {
        var qty = jQuery("#qty_"+id).val();
        var price = jQuery("#price_"+id).html();
        var priced = jQuery("#priced_"+id).html();
        jQuery("#total_"+id).html(parseInt(qty) * parseFloat(price));
        jQuery("#totald_"+id).html(parseInt(qty) * parseFloat(priced));
        
        CalculateTotal();
        ReFormat();
    }

    function CalculateTotal(){
        var totals = jQuery('.total');
        var totalds = jQuery('.totald');
        var grandTotal = 0, grandTotald = 0;
        jQuery.each(totals, function(i){
            grandTotal += parseFloat(jQuery(totals[i]).html());
            grandTotald += parseFloat(jQuery(totalds[i]).html());
        });

        jQuery('#grandtotal').html(parseFloat(grandTotal));   
        jQuery('#grandtotald').html(parseFloat(grandTotald));   
    }
    
    function ReFormat(){
        try{
        jQuery(".price").each(function(){
            jQuery("#"+this.id+"_show").html(formatNumber(parseFloat(jQuery(this).html()), 2));
        });
        jQuery(".priced").each(function(){
            jQuery("#"+this.id+"_show").html(formatNumber(parseFloat(jQuery(this).html()), 2));
        });
        jQuery(".total").each(function(){
            jQuery("#"+this.id+"_show").html(formatNumber(parseFloat(jQuery(this).html()), 2));
        });
        jQuery(".totald").each(function(){
            jQuery("#"+this.id+"_show").html(formatNumber(parseFloat(jQuery(this).html()), 2));
        });
        
        jQuery("#grandtotal_show").html(formatNumber(parseFloat(jQuery("#grandtotal").html()), 2));
        jQuery("#grandtotald_show").html(formatNumber(parseFloat(jQuery("#grandtotald").html()), 2));
        }catch(e){
            alert(e);
        }
    }
</script>

<h3 class="title-popup">Master Item</h3>

<div style="padding:10px">
<div id="content">
    <div class="DivTable">
        <form name="fCari" id="fCari" action="" method="get" >
            <table class="GridTableFilter">
                <tr>
                    <td>
                        <?php
                        echo $fp;
                        ?>
                    </td>
                </tr>
            </table>
        </form>
        <br class="bersih" />
        
        <table id="itemTable" class="GridTable" style="width: 100%;">
            <tr class="GridHeader">
                <td>No.</td>
                <td>Description</td>
                <td>Qty</td>
                <td>Unit Price</td>
                <td>Total Price</td>
                <td>Unit Price Dollar</td>
                <td>Total Price Dollar</td>
            </tr>
            <?php
            if ($rows > 0) {
                $nomor = $offset;
                foreach ($data as $row) {
                    $nomor++;
                    $styleTR = "GridItem1";
            ?>
                    <tr class="<?php echo $styleTR; ?>">
                        <td><?=$nomor?></td>		
                        <td><span class="nama"><?=$row->nama_item?></span><span style="display: none;" class="item"><?=$row->id_item?></span></td>
                        <td><?=form_input(array("id" => "qty_$row->id_item", "class" => "qty"), 0, 
                                "onblur=\"UpdateTotals('$row->id_item');\" onkeyup=\"this.value=this.value.replace(/[^\d]/,'');\"");?>
                            &nbsp;<span class="satuan"><?=$row->satuan_beli?></span></td>
                        <td align="right">
                            <span style="float: left;">Rp. </span>
                            <span id="price_<?=$row->id_item?>" class="price"><?=$row->harga_jual?></span>
                            <span id="price_<?=$row->id_item?>_show"></span>
                        </td>
                        <td align="right">
                            <span style="float: left;">Rp. </span>
                            <span id="total_<?=$row->id_item?>" class="total">0</span>
                            <span id="total_<?=$row->id_item?>_show"></span>
                        </td>
                        <td align="right">
                            <span style="float: left;">$ </span>
                            <span id="priced_<?=$row->id_item?>" class="priced"><?=$row->harga_jual_dollar?></span>
                            <span id="priced_<?=$row->id_item?>_show"></span>
                        </td>
                        <td align="right">
                            <span style="float: left;">$ </span>
                            <span id="totald_<?=$row->id_item?>" class="totald">0</span>
                            <span id="totald_<?=$row->id_item?>_show"></span>
                        </td>
                    </tr>

            <?php } ?>
                <tr class="GridHeader">
                    <td align="right" colspan="4">Total</td>
                    <td align="right">
                        <span style="float: left;">Rp.  </span>
                        <span id="grandtotal">0</span>
                        <span id="grandtotal_show"></span>
                    </td>
                    <td>&nbsp;</td>
                    <td align="right">
                        <span style="float: left;">$ </span>
                        <span id="grandtotald">0</span>
                        <span id="grandtotald_show"></span>
                    </td>
                </tr>
            <?php
            } else {
                ?>
                <tr class="GridItem1">
                    <td colspan="7">Tidak ada data</td>
                </tr>

                <?php
            }
            ?>
        </table>
    </div>
</div>
<?php echo $paging; ?>
<br class="bersih" />
<br class="bersih" />

<div style="left: 0; bottom: 0; width: 100%; background: black; padding: 3px;">
    <button onclick="submit_item();">Save</button>&nbsp;
    <button onclick="close_gb();">Cancel</button>
</div>

</div>

<script>
    ReFormat();
</script>